#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
@File Name: 优化_基于列表可原地修改的特性实现更快速_stack4.py
@Author: LT
@Date: 2021/9/27 13:46
# -----------------------------------------------------------------------------------------------------
@Description: 
这个类的pop方法等同于使用索引删除位移为-1的值（在列表尾是栈顶的情况下）
# -----------------------------------------------------------------------------------------------------
"""


class Error(Exception):
    pass


class Stack:
    def __init__(self, start=[]):
        self.stack = []
        for x in start:
            self.push(x)

    def push(self, obj):
        self.stack.append(obj)                  # 列表的尾部定为栈的顶部

    def pop(self):
        if not self.stack:
            raise Error("空栈")
        return self.stack.pop()                 # 类似于取出stack[-1]并删除, list.pop是本身就自带的方法

    def top(self):
        if not self.stack:
            raise Error("空栈")
        return self.stack[-1]

    def empty(self):
        return not self.stack                   # 判断栈是否为空

    # 重载
    def __repr__(self):                         # 重载对栈对象的打印
        return "[Stack:%s]" % self.stack

    def __eq__(self, other):                    # "==" 与 "!=" 操作符的重载
        return self.stack == other.stack

    def __len__(self):                          # 对栈内元素个数获取
        return len(self.stack)

    def __add__(self, other):                   # "+" 操作符的重载
        return Stack(self.stack + other.stack)

    def __mul__(self, reps):                    # 实例 * reps(重复次数)
        return Stack(self.stack * reps)

    def __getitem__(self, offset):              # 重载取索引、切片、in、for操作
        return self.stack[offset]

    def __getattr__(self, name):                # 是我觉得很废话的一段重载
        return getattr(self.stack, name)


if __name__ == '__main__':
    pass
